import time

import cv2
import numpy as np
import pyautogui

# bobber_image = cv2.imread('hook_img/bobber3.png')  # 风暴峭壁使用
# bobber_image = cv2.imread('hook_img/bobber3.png')  # 贫瘠之地 44.7, 23.8
# bobber_image = cv2.imread('hook_img/bobber5.png')  # 索拉查盆地 (17.2, 60.7)
# bobber_image = cv2.imread('hook_img/bobber6.png')  # 索拉查盆地 (17.2, 60.7)
# bobber_image = cv2.imread('hook_img/bobber7.png')  # 龙骨荒野 (27.3, 36.7) 站水里面向南
# bobber_image = cv2.imread('hook_img/bobber8.png')  # 凄凉之地 (742, 71.8)
# bobber_image = cv2.imread('hook_img/bobber10.png')   # 艾萨拉 (13.3, 51.5)

bobber_images = [
    # cv2.imread('hook_img/bobber3.png'),  # 风暴峭壁使用
    # cv2.imread('hook_img/bobber3.png'),  # 贫瘠之地 44.7, 23.8
    # cv2.imread('hook_img/bobber5.png'),  # 索拉查盆地 (17.2, 60.7)
    # cv2.imread('hook_img/bobber6.png'),  # 索拉查盆地 (17.2, 60.7)
    # cv2.imread('hook_img/bobber7.png'),  # 龙骨荒野 (27.3, 36.7) 站水里面向南
    # cv2.imread('hook_img/bobber8.png'),  # 凄凉之地 (742, 71.8)
    # cv2.imread('hook_img/bobber10.png'),  # 艾萨拉 (13.3, 51.5)
    # 赞加沼泽
    # cv2.imread('hook_img/bobber1.png'),
    cv2.imread('hook_img/bobber2.png'),
    # cv2.imread('hook_img/bobber3.png'),
    cv2.imread('hook_img/bobber9.png'),
    cv2.imread('hook_img/bobber10.png'),
]


# 这个脚本功能是在屏幕上找到浮标, 并点击
def click_bobber():
    screenshot = pyautogui.screenshot()
    screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
    # result是一个二维数组
    # 这个二维数组中的每个元素表示在输入图像（screenshot）中的对应位置与模板图像（target_image）的匹配程度。
    result = choose_result(screenshot)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result[0])

    # 匹配度比较高的示例
    # min_val = -0.35622406005859375, max_val=0.45643624663352966, min_loc=(1005, 11), max_loc=(872, 353)
    # min_val = -0.35637786984443665, max_val=0.46077489852905273, min_loc=(1005, 11), max_loc=(881, 357)
    # min_val = -0.3535726070404053, max_val=0.424777626991272, min_loc=(997, 971), max_loc=(1088, 493)
    # 当匹配度比较低时候的数据示例
    # min_val = -0.35622039437294006, max_val=0.3448461592197418, min_loc=(1005, 11), max_loc=(1452, 203)
    # min_val = -0.3535724878311157, max_val=0.3937825858592987, min_loc=(997, 971), max_loc=(0, 227)
    top_left = max_loc  # 这个是匹配图片的左上角, 这里一般点不着, 需要找到匹配图片的中间位置
    # 找到匹配图片的中心点
    h, w = result[1].shape[:2]
    bottom_right = (top_left[0] + w, top_left[1] + h)
    # center_x = (top_left[0] + bottom_right[0]) // 2
    # center_y = (top_left[1] + bottom_right[1]) // 2
    center_x = top_left[0] + (w * 0.7)
    center_y = top_left[1] + (h * 0.7)
    print(f"using max confidence bobber location: confidence={max_val}, x={center_x}, y={center_y}")
    # 点击匹配的图片中心点
    pyautogui.moveTo(center_x, center_y)  # 先移动过来, 再右击, 这样就不会让游戏误判导致拖拉屏幕了
    time.sleep(1)
    pyautogui.rightClick(center_x, center_y)


def choose_result(screenshot):
    result = None
    result_image = None
    max_val = 0
    for bobber_image in bobber_images:
        this_result = cv2.matchTemplate(screenshot, bobber_image, cv2.TM_CCOEFF_NORMED)
        min_val, this_max_val, min_loc, max_loc = cv2.minMaxLoc(this_result)
        print(f"bobber location: confidence={this_max_val}")
        if this_max_val > max_val:
            result = this_result
            result_image = bobber_image
            max_val = this_max_val
    return result, result_image


click_bobber()
